﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using PRET.ReportDataDefine.UIData;
using PRET.ReportControlLib.ServiceReference;

namespace PRET.ReportControlLib.ReportView
{
    public partial class TeamAttendanceEditorReportView : UserControl
    {
        private PRETWebServiceClient adapter = null;

        public TeamAttendanceEditorReportView()
        {
            InitializeComponent();
            adapter = new PRETWebServiceClient();
            Controls.ShowTeamAttEditorEventArgs args = new Controls.ShowTeamAttEditorEventArgs();
            args.StartYear = this.setYearPanelSim21.GetSelectedYearMonth;
            setYearPanelSim21_switchMonthEvent(null, args);
        }

        private void teamAttendanceMonth1_modifyHoursEvent(object sender, Controls.ModifyTeamHoursEventArgs args)
        {
            try
            {
                this.teamAttendanceMonth1.EnableControls(false);
                this.setYearPanelSim21.EnableControls(false);
                if (this.adapter == null)
                {
                    MessageBox.Show("TeamAttendanceEditorReportView没有初始化Server Adapter!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                this.adapter.SetTeamAttendanceHours(args.AHours, args.BHours, args.CHours, args.DHours, args.EHours, args.FHours, this.setYearPanelSim21.GetSelectedYearMonth);

                this.reportViewer.LocalReport.DataSources.Clear(); //clear report
                // 从后台获取高低柜状态列表
                TeamAttendanceRestHours[] dataList = this.adapter.GetTeamAttendanceHoursList(this.setYearPanelSim21.GetSelectedYearMonth);
                if (dataList != null && dataList.Length > 0)
                {
                    Microsoft.Reporting.WinForms.ReportDataSource dataset = new Microsoft.Reporting.WinForms.ReportDataSource("TeamAttendanceRestHoursDS"); // set the datasource
                    this.reportViewer.LocalReport.DataSources.Add(dataset);
                    dataset.Value = dataList.ToList<TeamAttendanceRestHours>();
                }
                this.reportViewer.LocalReport.Refresh();
                this.reportViewer.RefreshReport();
            }
            catch (Exception ex)
            {
                string msg = string.Format("TeamAttendanceEditorReportView.teamAttendanceMonth1_modifyHoursEvent() Failed: {0}.\r\nDetail: {1}", ex.Message, ex.StackTrace);
                MessageBox.Show(msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.teamAttendanceMonth1.EnableControls(true);
                this.setYearPanelSim21.EnableControls(true);
            }
        }

        private void setYearPanelSim21_switchMonthEvent(object sender, Controls.ShowTeamAttEditorEventArgs args)
        {
            try
            {
                this.teamAttendanceMonth1.EnableControls(false);
                this.setYearPanelSim21.EnableControls(false);
                if (this.adapter == null)
                {
                    MessageBox.Show("TeamAttendanceEditorReportView没有初始化Server Adapter!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                this.reportViewer.LocalReport.DataSources.Clear(); //clear report
                // 从后台获取高低柜状态列表
                TeamAttendanceRestHours[] dataList = this.adapter.GetTeamAttendanceHoursList(args.StartYear);
                if (dataList != null && dataList.Length > 0)
                {
                    Microsoft.Reporting.WinForms.ReportDataSource dataset = new Microsoft.Reporting.WinForms.ReportDataSource("TeamAttendanceRestHoursDS"); // set the datasource
                    this.reportViewer.LocalReport.DataSources.Add(dataset);
                    dataset.Value = dataList.ToList<TeamAttendanceRestHours>();
                }
                this.reportViewer.LocalReport.Refresh();
                this.reportViewer.RefreshReport();
            }
            catch (Exception ex)
            {
                string msg = string.Format("TeamAttendanceEditorReportView.setYearPanelSim21_switchMonthEvent() Failed: {0}.\r\nDetail: {1}", ex.Message, ex.StackTrace);
                MessageBox.Show(msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.teamAttendanceMonth1.EnableControls(true);
                this.setYearPanelSim21.EnableControls(true); 
            }
        }
    }
}
